<?php
// Lisää toiminnallisuus
include('functions.php');

if(isset($_POST['insert']))
{
    $table = $_POST['table'];

    $stmt = $pdo->prepare('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME != ?');
    $stmt->execute(array($table,"id"));
    
    $i = 0; unset($columns);
    while($result = $stmt->fetchObject())
        $columns[$i++] = $result->column_name;
    $last = count($columns) - 1;
    
    foreach($columns as &$column)
        if(strlen($_POST[$column]) > 0)
            $param[] = $_POST[$column];
        else
            $param[] = null;

    $query = "INSERT INTO " . $table . " (";
    for($i = 0; $i < $last; ++$i)
        $query .= $columns[$i] . ",";
    $query .= $columns[$last] . ")";
    $query .= " VALUES (";
    for($i = 0; $i < $last; ++$i)
        $query .= "?,";
    $query .= "?)";

    echo $query;
    var_dump($param);
    
    $stmt = $pdo->prepare($query);
    $stmt->execute($param);
    
    header('Location: admin.php');
}

if(isset($_POST['update']))
{
    $table = $_POST['table'];
    $id    = $_POST['id'];

    $stmt = $pdo->prepare('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME != ?');
    $stmt->execute(array($table,"id"));
    
    $i = 0; unset($columns);
    while($result = $stmt->fetchObject())
        $columns[$i++] = $result->column_name;
    $last = count($columns) - 1;
    
    foreach($columns as &$column)
        if(strlen($_POST[$column]) > 0)
            $param[] = $_POST[$column];
        else
            $param[] = null;
    $param[] = $id;

    $query = "UPDATE " . $table . " SET (";
    for($i = 0; $i < $last; ++$i)
        $query .= $columns[$i] . ",";
    $query .= $columns[$last] . ")";
    $query .= " = (";
    for($i = 0; $i < $last; ++$i)
        $query .= "?,";
    $query .= "?) WHERE ID = ?";

    echo $query;
    var_dump($param);
    
    $stmt = $pdo->prepare($query);
    $stmt->execute($param);
    
    header('Location: admin.php');
}

if(isset($_POST['delete']))
{
    $table = $_POST['table'];
    $id    = $_POST['id'];
    
    $query = "DELETE FROM $table WHERE ID = ?";

    echo $query;

    $stmt = $pdo->prepare($query);
    $stmt->execute(array($id));
    
    header('Location: admin.php');
}

?>
<!DOCTYPE html>
<html lang="fi">
    <head>
        <meta charset="utf-8">
        <title>O4 - admin</title>
        <link id="style" rel="stylesheet" type="text/css" href="style.css" media="screen" />
        <script type="text/javascript">    
            function toggle(node)
            {
                var parent = node.parentNode;
                var ul = parent.getElementsByTagName('ul')[0];
                if(ul.style.display == 'none')
                {
                    node.style.fontWeight = 'normal';
                    ul.style.display = 'inline';
                }
                else
                {
                    node.style.fontWeight = 'bold';
                    ul.style.display = 'none';
                }
            }
        </script>
    </head>
    <body>
        <? if(strlen($error) > 0) echo "<ul>$error</ul>"; ?>
        <ul class="layout">
            <?
                $stmt = $pdo->prepare('SELECT TABLENAME FROM PG_TABLES WHERE TABLEOWNER = ?');
                $stmt->execute(array('rasku'));
                
                while($result = $stmt->fetchObject())
                {
                    $table = $result->tablename;
                
                    $stmt2 = $pdo->prepare('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND COLUMN_NAME != ?');
                    $stmt2->execute(array($table,"id"));
                    
                    $i = 0; unset($columns);
                    while($result = $stmt2->fetchObject())
                        $columns[$i++] = $result->column_name;
                    ?>
                        <li>
                            <fieldset>
                                <legend onclick="toggle(this)">Manage <? echo e($table); ?></legend>
                                <ul class="layout">
                                    <li>
                                        <form method="post" action="admin.php">
                                            <table>
                                                <thead>
                                                    <tr>
                                                        <th>Insert into</th>
                                                        <th><input type="text" name="table" readonly="readonly" value="<? echo e($table); ?>" /></th>
                                                    </tr>
                                                </thead>
                                                <tfoot>
                                                    <tr>
                                                        <td><input type="reset" name="reset" value="Reset" /></td>
                                                        <td><input type="submit" name="insert" value="Insert" /></td>
                                                    </tr>
                                                </tfoot>
                                                <tbody>
                                                    <?
                                                        foreach($columns as &$column)
                                                            echo "<tr><td>" . e($column) . "</td><td><input type='text' name='" . e($column) . "' /></td></tr>";
                                                    ?>
                                                </tbody>
                                            </table>
                                        </form>
                                    </li>
                                    <li>
                                        <table>
                                            <thead>
                                                <tr>
                                                    <th>id</th>
                                                    <?
                                                        foreach($columns as &$column)
                                                            echo "<th>" . e($column) . "</th>";
                                                    ?>
                                                    <th></th>
                                                    <th></th>
                                                    <th></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?
                                                    $query = "SELECT * FROM $table ORDER BY ID ASC";
                                            
                                                    $stmt2 = $pdo->prepare($query);
                                                    $stmt2->execute();
                                        
                                                    while($result = $stmt2->fetch(PDO::FETCH_ASSOC))
                                                    {
                                                        ?>
                                                            <tr>
                                                                <form method="post" action="admin.php">
                                                                    <td><input type="text" name="id" readonly="readonly" value="<? echo e($result['id']); ?>" /></td>
                                                                    <?
                                                                        foreach($columns as &$column)
                                                                            echo "<td><input type='text' name='" . e($column) . "' value='" . e($result[$column]) . "' /></td>";
                                                                    ?>
                                                                    <td><input type="reset" name="reset" value="Reset" /></td>
                                                                    <td><input type="submit" name="update" value="Update" /></td>
                                                                    <td><input type="submit" name="delete" value="Delete" /></td>
                                                                    <input type="hidden" name="table" value="<? echo e($table); ?>" />
                                                                </form>
                                                            </tr>
                                                        <?
                                                    }
                                                ?>
                                            </tbdody>
                                        </table>
                                    </li>
                                </ul>
                            </fieldset>
                        </li>
                    <?
                }
            ?>
        </ul>
    </body>
</html>
